//
//  RegisterViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *imageLogo;
@property (nonatomic, strong) UITextField *accountFireld;
@property (nonatomic, strong) UITextField *passworkField;
@property (nonatomic, strong) UITextField *againField;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *backLoginBtn;
@property (nonatomic, strong) UIView *backView;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.imageLogo];
    [self.view addSubview:self.backView];
    
    // 键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}

//方法里对键盘弹出和将要消失做处理
- (void)keyboardShow:(NSNotification*)notification{
    //NSLog(@"显示键盘:%@",notification);
    //NSLog(@"%@",notification.userInfo);
    NSValue *keyboardEndFrameValue = notification.userInfo[UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardEndFrame;
    [keyboardEndFrameValue getValue:&keyboardEndFrame];
    
    // 往上移动
    [UIView animateWithDuration:0.2 animations:^{
        // 改变高度
        self.backView.frame = CGRectMake(0,50, ScreenW, ScreenH-10);
        self.backView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
        
    }];
}

- (void)keyboardHide:(NSNotification*)notification{
    
    //NSLog(@"键盘消失:%@",notification);
    [UIView animateWithDuration:0.2 animations:^{
        
        self.backView.frame = CGRectMake(0, CGRectGetMaxY(self.imageLogo.frame)+40*ScaleW, ScreenW,ScreenH-CGRectGetHeight(self.imageLogo.frame));
        self.backView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
        
    }];
}


#pragma make - UITextFieldDelegater

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    NSLog(@"%@",textField.text);
    if (textField.text.length < 11 && textField.text.length > 0 && textField == self.accountFireld) {
        [self alertShow:@"提示" message:@"请输入正确的手机号码"];
//        [self showHint:CustomLocalizedString(@"请输入正确的手机号码", nil)];
        
    }else if (textField.text.length < 6 && textField.text.length > 8 &&textField == self.passworkField){
        [self alertShow:@"提示" message:@"密码要大于6位数小于8位数"];
    }
    else if (textField == self.againField){
        
        if (![self.againField.text isEqualToString:self.passworkField.text]) {
            [self alertShow:@"提示" message:@"两次输入的密码不相同"];
//            [self showHint:CustomLocalizedString(@"两次输入的密码不相同", nil)];
        }
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.accountFireld) {
        
        NSString * text  = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (text.length == 11) {
            
//            self.accountFireld.text = [text substringToIndex:text.length-1];
            //NSLog(@"%@ len:%@",string,textField.text);
            NSString *regex = @"^((13[0-9])|(147)|(17[0,1][6,8])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            BOOL isMatch = [pred evaluateWithObject:text];
            NSLog(@"is:%d",isMatch);
            if (isMatch == false) {
//                [self showHint:CustomLocalizedString(@"请输入正确的手机号码", nil)];
                [self alertShow:@"提示" message:@"请输入正确的手机号码"];
            }
        }else if (textField.text.length >= 11){
            [self alertShow:@"提示" message:@"号码格式不正确"];
//            [self showHint:CustomLocalizedString(@"号码格式不正确", nil)];
            
        }
        
    }
    return YES;
}

-(void)alertShow:(NSString *)title message:(NSString *)message{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}


-(UIImageView *)imageLogo{
    if (!_imageLogo) {
        _imageLogo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 110*ScaleW, 40*ScaleW)];
        _imageLogo.center = CGPointMake(ScreenW/2, 120);
        _imageLogo.image = [UIImage imageNamed:@"register_bg"];
        
    }
    return _imageLogo;
}

-(UIView *)backView{
    
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageLogo.frame)+40*ScaleW, ScreenH, ScreenH - CGRectGetHeight(self.imageLogo.frame))];
        UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(35*ScaleW, 5*ScaleW, ScreenW - (35*2*ScaleW), 42)];
        image1.image = [UIImage imageNamed:@"et_bg_login.9"];
        [_backView addSubview:image1];
        [_backView addSubview:self.accountFireld];
        UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(35*ScaleW, CGRectGetMaxY(image1.frame)+ 28, ScreenW - (35*2*ScaleW), 42)];
        image2.image = [UIImage imageNamed:@"et_bg_login.9"];
        [_backView addSubview:image2];
        [_backView addSubview:self.passworkField];
        UIImageView *image3 = [[UIImageView alloc]initWithFrame:CGRectMake(35*ScaleW, CGRectGetMaxY(image2.frame)+ 28, ScreenW - (35*2*ScaleW), 42)];
        image3.image = [UIImage imageNamed:@"et_bg_login.9"];
        [_backView addSubview:image3];
        [_backView addSubview:self.againField];
        [_backView addSubview:self.loginBtn];
        [_backView addSubview:self.backLoginBtn];
        
    }
    return _backView;
}

-(UITextField *)accountFireld{
    if (!_accountFireld) {
        _accountFireld = [[UITextField alloc]initWithFrame:CGRectMake(40*ScaleW, 5*ScaleW, ScreenW - (40*2*ScaleW), 40)];
        //        _accountFireld.layer.borderWidth = 1;
        //        _accountFireld.layer.masksToBounds = YES;
        _accountFireld.layer.borderColor = [UIColor grayColor].CGColor;
        _accountFireld.layer.shadowColor = [UIColor grayColor].CGColor;
        //        _accountFireld.layer.shadowOffset = CGSizeMake(1.0, 1.0);
        //        _accountFireld.layer.shadowOpacity = YES;
        _accountFireld.delegate = self;
        _accountFireld.placeholder = @"请输入手机号码";
        
    }
    return _accountFireld;
}

-(UITextField *)passworkField{
    if (!_passworkField) {
        _passworkField = [[UITextField alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.accountFireld.frame)+30, ScreenW - (40*2*ScaleW), 40)];
        //        _passworkField.layer.borderWidth = 1;
        _passworkField.layer.borderColor = [UIColor grayColor].CGColor;
        _passworkField.layer.shadowColor = [UIColor grayColor].CGColor;
        //        _passworkField.layer.shadowOffset = CGSizeMake(1.0, 1.0);
        //        _passworkField.layer.shadowOpacity = YES;
        _passworkField.placeholder = @"请设置登录密码";
        _passworkField.secureTextEntry = YES;
        _passworkField.delegate = self;
        _passworkField.textColor = [UIColor grayColor];
        
    }
    return _passworkField;
}

-(UITextField *)againField{
    if (!_againField) {
        _againField = [[UITextField alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.passworkField.frame)+30, ScreenW - (40*2*ScaleW), 40)];
        _againField.placeholder = @"请再次输入名密码";
        _againField.secureTextEntry = YES;
        _againField.delegate = self;
        _againField.tintColor = [UIColor grayColor];
    }
    return _againField;
}

-(UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.againField.frame)+40, ScreenW - (40*2*ScaleW), 40)];
        [_loginBtn setTitle:@"注册" forState:UIControlStateNormal];
        _loginBtn.backgroundColor = MainColor;
        _loginBtn.layer.cornerRadius = 2.0;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn addTarget:self action:@selector(registercCick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _loginBtn;
}

-(UIButton *)backLoginBtn{
    if (!_backLoginBtn) {
        _backLoginBtn = [[UIButton alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.loginBtn.frame)+15, 35, 20)];
        [_backLoginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_backLoginBtn setTitleColor:MainColor forState:UIControlStateNormal];
        _backLoginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_backLoginBtn addTarget:self action:@selector(btnClickBack:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backLoginBtn;
}

-(void)registercCick{
    if (self.accountFireld.text.length == 11 && [self.passworkField.text isEqualToString:self.againField.text]) {
            
            NSString *url = [NSString stringWithFormat:@"%@%@",MAIN_URL,REGISTER_URL];
            NSDictionary * dict = @{@"member_phone":self.accountFireld.text,@"member_pass":self.passworkField.text};
            [NetworkRequestManager postWithUrl:url parameter:dict success:^(id requestData) {
                NSLog(@"%@",requestData);
            if ([requestData[@"code"] isEqualToString:@"20002"]) {
                    [self alertShow:@"提示" message:@"电话号码已注册过"];
                    
                }else if ([requestData[@"code"] isEqualToString:@"10000"]){
                    [self alertShow:@"提示" message:@"注册成功"];
                }
                
            } failure:^(NSError *error) {
                [self alertShow:@"提示" message:@"注册失败"];
            }];
    }
    else {
        [self alertShow:@"提示" message:@"填写信息有误"];
    }
}


-(void)btnClickBack:(UIButton *)sender{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [self.accountFireld resignFirstResponder];
    [self.passworkField resignFirstResponder];
    [self.againField resignFirstResponder];
}



@end
